import javax.swing.*;

public class GuessNumber {
    static   Integer indexNumber =  7;

    //随机生成数
    static   Integer num=(int)(Math.random()*101);
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("猜数字");
        jFrame.setBounds(600, 300,300 , 300);

        JPanel jPanelBig = new JPanel();
        Box verticalBoxBig = Box.createVerticalBox();

        JPanel jPanel = new JPanel();
        Box verticalBox = Box.createVerticalBox();

        JLabel jLabel = new JLabel();
        jLabel.setText("请输入您要猜的数字");

        JTextField jTextField = new JTextField();
        jTextField.setSize(100,10);

        verticalBox.add(jLabel);
        verticalBox.add(jTextField);

        jPanel.add(verticalBox);

        JPanel jPanel1 = new JPanel();
        Box horizontalBox = Box.createHorizontalBox();

        JButton button_1 = new JButton("确认");
        button_1.addActionListener(e->{
            if(indexNumber==1){
                indexNumber = 7;
                num = (int)(Math.random()*101);
                JDialog jDialog = new JDialog();
                Box verticalBox1 = Box.createVerticalBox();

                jDialog.setBounds(700, 400,300 , 100);
                JLabel jLabel1 = new JLabel("很遗憾机会用完了！点击X重新开始游戏");
                verticalBox1.add(jLabel1);
                jDialog.add(verticalBox1);
                jDialog.setVisible(true);
            }else {
                try {
                    if(Integer.parseInt(jTextField.getText())==num){
                        indexNumber = 7;
                        num = (int)(Math.random()*101);
                        JDialog jDialog = new JDialog();
                        jDialog.setBounds(700, 400,300 , 100);
                        JLabel jLabel1 = new JLabel("恭喜你才对了 数字刷新 次数重置");
                        jDialog.add(jLabel1);
                        jDialog.setVisible(true);
                        return;
                    }
                    if(Integer.parseInt(jTextField.getText())<num){
                        indexNumber = indexNumber-1;
                        JDialog jDialog = new JDialog();
                        jDialog.setBounds(700, 400,300 , 100);
                        JLabel jLabel1 = new JLabel("猜小了 你还有"+indexNumber+"机会");
                        jDialog.add(jLabel1);
                        jDialog.setVisible(true);
                    }
                    if(Integer.parseInt(jTextField.getText())>num){
                        indexNumber = indexNumber-1;
                        JDialog jDialog = new JDialog();
                        jDialog.setBounds(700, 400,300 , 100);
                        JLabel jLabel1 = new JLabel("猜大了 你还有"+indexNumber+"机会");
                        jDialog.add(jLabel1);
                        jDialog.setVisible(true);
                    }
                }catch (Exception q){
                    JDialog jDialog = new JDialog();
                    jDialog.setBounds(700, 400,300 , 100);
                    JLabel jLabel1 = new JLabel("请输入数字");

                    jDialog.add(jLabel1);
                    jDialog.setVisible(true);
                }
            }
        });

        JButton button_2 = new JButton("重玩");
        button_2.addActionListener(e -> {
            indexNumber = 7;
            num = (int)(Math.random()*101);
            JDialog jDialog = new JDialog();
            jDialog.setBounds(700, 400,300 , 100);
            JLabel jLabel1 = new JLabel("重玩触发 机会刷新 次数刷新");
            jDialog.add(jLabel1);
            jDialog.setVisible(true);
        });

        JButton button_3 = new JButton("结束");
        button_3.addActionListener(e -> {
            System.exit(0);
        });
        horizontalBox.add(button_1);
        horizontalBox.add(button_2);
        horizontalBox.add(button_3);

        jPanel.add(verticalBox);
        jPanel1.add(horizontalBox);


        verticalBoxBig.add(jPanel);
        verticalBoxBig.add(jPanel1);

        jPanelBig.add(verticalBoxBig);

        jFrame.add(jPanelBig);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }
}
